const fs = require('fs')
const path = require('path')
const http = require('http')

const server = http.createServer()
server.on('request', (req, res) => {
  // 获取客服端请求的url地址
  const url = req.url
  console.log(url)
  // const fpath = path.join(__dirname, url)
  let fpath = ''
  if (url === '/') {
    fpath = path.join(__dirname, './dist/index.html')
  } else {
    fpath = path.join(__dirname, './dist', url)
  }
  console.log(fpath)
  fs.readFile(fpath, 'utf8', (err, dataStr) => {
    if (err) return res.end('404 Not Found')
    res.end(dataStr)
  })
})
server.listen(80, (req, res) => {
  console.log('server is running http://127.0.0.1')
})
